package com.voxel.sense.infomsg.module.task.check;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.com.common.app.BViewActivity;
import com.example.com.common.utils.ListUtil;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.view.EmptyView;
import com.example.com.common.view.dialog.IDialog;
import com.example.com.common.view.dialog.InputDialogCallBack;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.MultiItemBean;
import com.voxel.sense.infomsg.module.task.execution.report.details.DetailsReportActivity;
import com.voxel.sense.infomsg.module.task.execution.report.details.TaskReportContract;
import com.voxel.sense.infomsg.view.TaskChecktemDivider;
import com.voxel.sense.infomsg.widget.CommonImagePagerActivity;
import com.voxel.sense.infomsg.widget.adapter.FileGriviewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.Jzvd;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.IntentConstants;

/**
 * 审核任务
 */
public class CheckTaskActivity extends BViewActivity<CheckTaskContract.Presenter>
        implements CheckTaskContract.View {

    @BindView(R.id.recycle_view)
    RecyclerView recycle_view; //任务列表
    @BindView(R.id.titlebar_tv_left)
    TextView titlebar_tv_left; //返回
    @BindView(R.id.tv_pass)
    TextView tv_pass; //通过
    @BindView(R.id.tv_reject)
    TextView tv_reject; //驳回
    @BindView(R.id.empty_view)
    EmptyView empty_view;
    @BindView(R.id.check_desc)
    TextView mCheckDesc;
    private String mTaskNumber;

    private FileGriviewAdapter mImageAdapter;//图片展示的适配器
    private FileGriviewAdapter mDocuAdapter;//文件展示的适配器
    private FileGriviewAdapter mVideoAdapter;//文件展示的适配器
    private boolean mCheckStatus;
    private CheckTaskAdapter mAdapter;
    private String mTaskId;
    private int openType;


    public static void show(Context context, String TaskNumb, String taskId, int operatorType) {
        if (TaskNumb == null) {
            return;
        }
        Intent intent = new Intent(context, CheckTaskActivity.class);
        intent.putExtra(IntentConstants.TASK_NUMB, TaskNumb);
        intent.putExtra(IntentConstants.STRING_ID, taskId);
        intent.putExtra(IntentConstants.OPERATORTYPE, operatorType);
        context.startActivity(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.resetAllVideos();
    }

    @Override
    protected CheckTaskContract.Presenter initPresenter() {
        return new CheckTaskPresenter(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_check_task;
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        mTaskNumber = getIntent().getStringExtra(IntentConstants.TASK_NUMB);
        mTaskId = getIntent().getStringExtra(IntentConstants.STRING_ID);
        openType = getIntent().getIntExtra(IntentConstants.OPERATORTYPE, 0);

        if (TextUtils.isEmpty(mTaskNumber)) {
            ToastUtil.showToastLong(Factory.app(), "界面跳转发生错误");
            finish();
            return false;
        }
        return super.initArgs(bundle);
    }

    @Override
    protected void initData() {
        super.initData();
        mAdapter = new CheckTaskAdapter();
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type = mAdapter.getItems().get(position).getType();
                if (type == CheckTaskContract.TYPE_FILE || type == CheckTaskContract.TYPE_PHOTO) {
                    return 1;
                }
                return 3;
            }
        });
        recycle_view.setLayoutManager(manager);
        recycle_view.addItemDecoration(new TaskChecktemDivider());
        recycle_view.setAdapter(mAdapter);
        empty_view.bind(recycle_view);
        showPassBtn();
    }

    private void showPassBtn() {
        switch (openType){
            case 0:
                tv_pass.setVisibility(View.VISIBLE);
                tv_reject.setVisibility(View.VISIBLE);
                break;
            case 1:
                tv_pass.setVisibility(View.INVISIBLE);
                tv_reject.setVisibility(View.INVISIBLE);
                break;
            default:
                tv_pass.setVisibility(View.VISIBLE);
                tv_reject.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void initEven() {
        super.initEven();
        empty_view.triggerLoading();
        mPresenter.doLoadTaskFile(mTaskNumber);
        mAdapter.setListener(new RecyclerAdapter.AdapterListenerImpl<MultiItemBean<String>>() {
            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, MultiItemBean<String> bean) {
                super.onItemClick(holder, bean);
                if (bean.getType() == CheckTaskContract.TYPE_REPORT) {
                    //查看报表
                    DetailsReportActivity.show(CheckTaskActivity.this, bean.getExtra(), TaskReportContract.LOOK_REPORT, mTaskNumber);
                } else if (bean.getType() == CheckTaskContract.TYPE_PHOTO) {
                    int position = recycle_view.getChildAdapterPosition(holder.itemView);
                    //查看图片大图
                    mPresenter.doShowPageGallery(mAdapter.getItems(), position);
                } else if (bean.getType() == CheckTaskContract.TYPE_FILE) {
                    mPresenter.doLookDocFile(bean.getData());
                    //查看文件
                }
            }
        });

        empty_view.addRefreshBtnEvent(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                empty_view.triggerLoading();
                mPresenter.doLoadTaskFile(mTaskNumber);
            }
        });
    }

    @OnClick({R.id.titlebar_tv_left, R.id.tv_pass, R.id.tv_reject})
    void clickEvent(View v) {
        int id = v.getId();
        if (R.id.titlebar_tv_left == id) {
            onBackPressed();
        } else if (R.id.tv_pass == id) {
            mPresenter.doSubmitPass(mTaskNumber, mTaskId);
        } else if (R.id.tv_reject == id) {
//            mPresenter.doSubmitUnpass(mTaskNumber);
            showInputDialog();
        }

    }

    private void showInputDialog() {
        IDialog.getInstance().showInputDialog(this, "驳回任务", "请输入驳回的原因", "取消", "确定", false, true, new InputDialogCallBack() {
            @Override
            public void onClick(int what, String inputText) {
                if (IDialog.YES == what) {
                    if (!TextUtils.isEmpty(inputText)) {
                        mPresenter.doSubmitUnpass(mTaskNumber, mTaskId, inputText);
                    } else {
                        ToastUtil.showToastLong(Factory.app(), "请输入与驳回原因");
                    }
                }
            }
        });
    }

    @Override
    public void onRefreshView(List list, String desc) {
        empty_view.triggerOkOrEmpty(!ListUtil.isEmpty(list));
        mAdapter.replace(list);
        mCheckDesc.setText(desc+"");
    }

    @Override
    public void onShowPageGallery(List<String> datas, int position) {
        CommonImagePagerActivity.showImages(this, (ArrayList<String>) datas, position);
    }

    @Override
    public void onSubmitCheck(boolean b) {
        finish();
    }

    @Override
    public void showEmptyView() {
        empty_view.triggerEmpty();
    }

}
